package com.htx.zqs.blesmartmask.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.LineData;
import com.google.gson.Gson;
import com.htx.zqs.blesmartmask.R;
import com.htx.zqs.blesmartmask.bean.AfterCourseData;
import com.htx.zqs.blesmartmask.bean.BeforeCourseData;
import com.htx.zqs.blesmartmask.bean.ChartInfoBean;
import com.htx.zqs.blesmartmask.bean.Contants;
import com.htx.zqs.blesmartmask.bean.ThirdItemInfoBean;
import com.htx.zqs.blesmartmask.custom.MpLineChart;
import com.htx.zqs.blesmartmask.custom.mpandroidchart.AfterMpChartManger;
import com.htx.zqs.blesmartmask.custom.mpandroidchart.BeforeMpChartManger;
import com.htx.zqs.blesmartmask.listeners.OkListener;
import com.htx.zqs.blesmartmask.utils.BlEUtile;
import com.htx.zqs.blesmartmask.utils.LocationUtil;
import com.htx.zqs.blesmartmask.utils.OkhttpUtils;
import com.htx.zqs.blesmartmask.utils.Utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by KomoriWu
 * on 2019/2/28.
 */
public class ChartActivity extends BaseActivity {

    private static final String FAT_WB = "fatwbtwo.link";
    @InjectView(R.id.ib_back)
    ImageButton ibBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    private String courseDate;
    private String deviceid;
    private String courseName;
    private int mLayoutType;
    private Activity context;
    private MpLineChart beforeChart;
    private MpLineChart afterChart;
    private OkhttpUtils mOkhttpUtils;
    private List<ChartInfoBean.DataBean.UserInfoBean.AfterCourseDataBean> afterCourseDataBeans;
    private List<AfterCourseData> mAfterCourseData = new ArrayList<>();
    private List<BeforeCourseData> mBeforeCourseData = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart);
        ButterKnife.inject(this);
        context = ChartActivity.this;
        ibBack.setVisibility(View.VISIBLE);
        initData();
    }

    private void initData() {
        courseName = getIntent().getStringExtra("courseName");
        courseDate = getIntent().getStringExtra("courseDate");
        mLayoutType = getIntent().getIntExtra("layoutType", 0);
        deviceid = LocationUtil.getUniquePsuedoID();
        tvTitle.setText(courseName);

        initView(1);

        if (BlEUtile.isNetworkConnected(context)) {
            getChartInfoFromALY();
        } else {
            queryDB();
            initView(1);
        }
    }

    private OkListener mListener = new OkListener() {
        @Override
        public void onSuccess(int requestCode, String string) {
            Gson gson = new Gson();
            ThirdItemInfoBean thirdItemInfoBean = gson.fromJson(string, ThirdItemInfoBean.class);
            ThirdItemInfoBean.DataBean thirdItemInfo = thirdItemInfoBean.getData();
            List<AfterCourseData> afterCourseDataList = thirdItemInfo.getAfterCourseData();
            List<BeforeCourseData> beforeCourseDataList = thirdItemInfo.getBeforeCourseData();
            if (afterCourseDataList != null) {
                Collections.sort(afterCourseDataList);
            }
            if (beforeCourseDataList != null) {
                Collections.sort(beforeCourseDataList);
            }
            Collections.sort(beforeCourseDataList);
            mAfterCourseData = afterCourseDataList;
            mBeforeCourseData = beforeCourseDataList;
            initView(2);
        }

        @Override
        public void onError(int requestCode, IOException e) {
            queryDB();
            initView(1);
        }
    };

    private void queryDB() {
        mAfterCourseData.clear();
        mBeforeCourseData.clear();
        mBeforeCourseData.addAll(BeforeCourseData.findOfflineData(courseDate));
        mAfterCourseData.addAll(AfterCourseData.findOfflineData(courseDate));
    }


    private HashMap mMap = new HashMap();

    private void getChartInfoFromALY() {
        mMap.clear();
        if (mOkhttpUtils == null) {
            mOkhttpUtils = new OkhttpUtils(context, Contants.GET_CHART_INFO_FROM_ALY, mListener);
        }
        mMap.put("name", Utils.getCourseName(courseDate));
        mMap.put("deviceid", deviceid);
        mMap.put("coursedate", courseDate);
        mMap.put("islogo", "A");
        mOkhttpUtils.postForm(Contants.GET_CHART_INFO_FROM_ALY, Contants.BASE_URL + FAT_WB, mMap);
    }

    private void initView(int type) {
        beforeChart = (MpLineChart) findViewById(R.id.before_chart);
        afterChart = (MpLineChart) findViewById(R.id.after_chart);
        TextView tv_chart = findViewById(R.id.tv_chart);
        TextView tv_chart_visivable = findViewById(R.id.tv_chart_visivable);
        Description description = new Description();
        description.setTextColor(R.color.colorPrimary);
        description.setText("");
        description.setTypeface(Typeface.MONOSPACE);

        description.setTextSize(12);
        description.setTextAlign(Paint.Align.RIGHT);
        BeforeMpChartManger.setLineName(context.getResources().getString(R.string.fat));
        BeforeMpChartManger.setLineName1(context.getResources().getString(R.string.water));
        beforeChart.setDescription(description);
        int count, num;
        if (mBeforeCourseData == null || mBeforeCourseData.size() == 0) {
            count = 0;
        } else {
            count = mBeforeCourseData.size();
        }
        Log.e("94====db=========", mBeforeCourseData + "");
        LineData lineData = BeforeMpChartManger.initDoubleLineChart(context, beforeChart, count, mBeforeCourseData);
        BeforeMpChartManger.initDataStyle(beforeChart, lineData, context, count, type);

        if (mLayoutType == 0) {
            tv_chart_visivable.setText(courseName);
            // TODO: 2018/12/11
            tv_chart.setVisibility(View.GONE);
            afterChart.setVisibility(View.GONE);
        } else {
            tv_chart_visivable.setText(context.getResources().getString(R.string.before_course));
            tv_chart.setVisibility(View.VISIBLE);
            afterChart.setVisibility(View.VISIBLE);
        }
        Description description1 = new Description();
        description1.setTextColor(R.color.colorPrimary);
        description1.setText("");
        description1.setTypeface(Typeface.MONOSPACE);
        description1.setTextSize(12);
        description1.setTextAlign(Paint.Align.RIGHT);
        AfterMpChartManger.setLineName(context.getResources().getString(R.string.fat));
        AfterMpChartManger.setLineName1(context.getResources().getString(R.string.water));
        afterChart.setDescription(description1);
        if (mAfterCourseData == null || mAfterCourseData.size() == 0) {
            num = 0;
        } else {
            num = mAfterCourseData.size();
        }
        LineData lineData1 = AfterMpChartManger.initDoubleLineChart(context, afterChart, num, mAfterCourseData);
        AfterMpChartManger.initDataStyle(afterChart, lineData1, context, num, type);
    }

    @OnClick(R.id.ib_back)
    public void onViewClicked() {
        finish();
    }

    public static void startAct(Context context, String courseName, String courseDate,
                                int layoutType) {
        Intent intent = new Intent(context, ChartActivity.class);
        intent.putExtra("courseName", courseName);
        intent.putExtra("courseDate", courseDate);
        intent.putExtra("layoutType", layoutType);
        context.startActivity(intent);
    }
}
